package cn.botter.subject.application.interceptor;


import cn.botter.subject.common.context.LoginContext;
import com.alibaba.druid.util.StringUtils;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @BelongsProject: bo-club
 * @BelongsPackage: cn.botter.gateway.interceptor
 * @Author: 爱写Bug的Botter
 * @CreateTime: 2025-03-29:20:46
 * @Description: 描述
 * @Version: 1.0
 */


/**
 * 自定义拦截器方法
 */
public class LoginInterceptor implements HandlerInterceptor {
    //请求之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String loginId = request.getHeader("loginId");
        if (!StringUtils.isEmpty(loginId)) {
            LoginContext.set("loginId", loginId);
        }
        return true;
    }
    //请求之后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        LoginContext.remove();
    }
}
